package perf.data;

import java.util.ArrayList;
import java.util.List;

public class Linecosts extends BaseCost implements BaseInterface, Comparable<Linecosts> {
	
	private ArrayList<Callcosts> calls = new ArrayList<Callcosts>();
	@SuppressWarnings("unchecked")
	private BaseItem parent;
	
	public Linecosts( int line ) {
		setLine( line );
	}
	
	public Linecosts( int line, long[] costs ) {
		super( costs );
		setLine( line );
	}
	
	public Callcosts getCallcostsInstance( Function dest ) {
		for ( Callcosts c : calls ) {
			if ( c.getDestination().getName().equals( dest.getName() ) ) {
				return c;
			}
		}
		
		return add( new Callcosts( dest ) );
	}
	
	protected Callcosts add( Callcosts cc ) {
		calls.add( cc );
		return cc;
	}
	
	@Override
	public long getCost( int idx ) {
		long costs = 0;
		for ( Callcosts cc : calls ) {
			costs += cc.getCost(idx);
		}
		return super.getCost( idx ) + costs;
	}
	
	public List<Callcosts> getCalls( ) {
		return calls;
	}
	
	public boolean hasCall( int idx ) {
		return calls.size() > idx && calls.get(idx) != null;
	}
	
	public Callcosts getCall( int idx ) {
		return calls.get( idx );	
	}
	

	@Override
	public void compact() {
		calls.trimToSize();
	}

	@Override
	public String getName() {
		return getLine() + "";
	}

	@SuppressWarnings("unchecked")
	@Override
	public void setParent(BaseItem parent) {
		this.parent = parent;
	}
	
	@SuppressWarnings("unchecked")
	public BaseItem getParent( ) {
		return parent;
	}

	@Override
	public int compareTo(Linecosts o) {
		return o.getLine() - this.getLine();
	}
}
